﻿using Core.Domain.Users.EventHandler;
using Core.Domain.Users.Events;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.EventBus
{
    public class TEventBus
    {

        private static Dictionary<string, List<Type>> _handlerFactories;

        static TEventBus() {
            _handlerFactories = new Dictionary<string, List<Type>>();
            RegisterEvents();
        }

        private static void RegisterEvents()
        {
            _handlerFactories.Add(typeof(FinishTaskEvent).Name, new List<Type>()
            {
                typeof(FinishTaskEventHandler)
            });
        }

        public static void Publish<T>(T e) where T : Event
        {
            var handlers = _handlerFactories[typeof(T).Name];
            foreach (var t in handlers)
            {
                IEventHandler<T> handler= Activator.CreateInstance(t) as IEventHandler<T>;
                handler?.Handle(e);
            }
        }
    }
}
